using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CreatingRainbows
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Player : Actor
    {

        private KeyboardState keyboard;
        private MouseState mouseState;

        public Game1 mainReference;

        //if its true then its the green circle, if false then it will be a cursor
        bool isPlayer;

        public Player(Game1 game, GameScene gameScene, bool isPlayer)
            : base(game)
        {
            this.isPlayer = isPlayer;

            mainReference = game;
            gameScene.Components.Add(this);
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (isPlayer)
            {
                KeyboardInput();
            }
            else
            {
                MouseInput();
            }

            base.Update(gameTime);
        }

        public void MouseInput()
        {
            mouseState = Mouse.GetState();

            this.position = new Vector2(mouseState.X, mouseState.Y);
        }

        private void KeyboardInput()
        {
            keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.W))
            {
                if (this.position.Y >= mainReference.ForegroundY)
                {
                    this.position.Y -= 5;
                }
            }
            if (keyboard.IsKeyDown(Keys.S))
            {
                if (this.position.Y <= ((mainReference.ScreenHeight) - (mainReference.ForegroundY)) - this.source.Height)
                {
                    this.position.Y += 5;
                }
            }
            if (keyboard.IsKeyDown(Keys.A))
            {
                if (this.position.X >= mainReference.ForegroundX)
                {
                    this.position.X -= 5;
                }
            }
            if (keyboard.IsKeyDown(Keys.D))
            {
                if (this.position.X <= ((mainReference.ScreenWidth) - (mainReference.ForegroundX)) - this.source.Width)
                {
                    this.position.X += 5;
                }
            }
        }
    }
}
